<template>
  <div ref="pageBody" class="pageBody">
    <div
      v-for="item in rowList"
      :key="item.key"
      class="item"
    >
      <component
        :is="item.key"
        :key="item.id"
        :page-id="pageId"
        :extend="extend"
        :content-width="contentWidth"
        v-bind="item"
        :query="query"
      />
    </div>
    <div class="clear" />
  </div>
</template>

<script>
import Vue from 'vue'
import moment from 'moment'
import { GetBody } from '@/customForm/api/page'
export default {
  components: {
  },
  props: {
    pageId: {
      type: String,
      default: null
    },
    extend: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      title: null,
      query: null,
      contentWidth: 1920,
      rowList: [],
      queryControl: [0, 1, 2, 4]
    }
  },
  computed: {},
  watch: {
    pageId: {
      handler(val) {
        if (val) {
          this.$nextTick(this.reset)
        }
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    moment,
    reset() {
      this.contentWidth = this.$refs.pageBody.offsetWidth - 20
      this.initForm()
    },
    async initForm() {
      const res = await GetBody(this.pageId, false)
      if (res.Controls != null) {
        const rows = []
        const q = {}
        res.Controls.forEach(e => {
          const t = this.format(e)
          if (this.queryControl.includes(t.type)) {
            q[t.id] = {
              ver: 1,
              param: []
            }
          }
          rows.push(t)
        })
        this.query = q
        this.rowList = rows
      } else {
        this.rowList = []
        this.query = null
      }
    },
    format(control) {
      const t = {
        key: 'c_' + control.Id,
        id: control.Id,
        title: control.Name,
        code: control.Code,
        width: control.WidthRatio,
        height: control.HeightGrid,
        show: control.Show,
        type: control.ControlType,
        item: null,
        children: null
      }
      if (t.type === 0) {
        t.item = control.Statistics
      } else if (t.type === 1) {
        t.item = control.Chart ?? {}
      } else if (t.type === 4) {
        t.item = control.Table ?? {}
      } else if (t.type === 5) {
        t.item = control.Layout ?? {}
        t.children = control.Children
      } else if (t.type === 6) {
        t.item = control.ControlSet ?? {}
      } else if (t.type === 3) {
        t.item = control.Search ?? {}
      } else if (t.type === 2) {
        t.item = control.CensusTable ?? {}
      }
      Vue.component(t.key, function(resolve, reject) {
        require(['@/' + control.ControlPath + '.vue'], resolve)
      })
      return t
    }
  }
}
</script>

<style scoped>
.pageBody {
  width: 100%;
  min-width: 1372px;
  background-color: #f0f0f0;
  padding: 10px;
}
.clear {
  clear: both;
}
.pageBody .item {
  float: left;
  margin: 5px;
  position: relative;
}
.dark .pageBody {
  background-color: #1d1e1f !important;

}
</style>
